package info.techgeek.labs.batch;

import java.io.*;

public class BatchInvoke {
	// Storing stream in global variable is bad idea;
	// Here I've used global variable to make example code
	// simple; don't use global variable in the real application

	static InputStream pi;

	public static void main(String args[]) {
		System.out.println(" Begin ");
		try {
			Process p;
			// Spawn the new process running aaa.bat script
			p = Runtime.getRuntime().exec("aaa.bat");
			pi = p.getInputStream();

			// Create thread for intercepting standard output
			// of the new process; Real application should also
			// catch standard error - the standard error handler
			// is omitted in the example for simplicity
			Thread t = new Thread(new Runnable() {
				public void run() {
					int c;
					try {
						for (;;) {
							c = pi.read();
							if (c < 0)
								break;
							System.out.write(c);
						}
					} catch (IOException ex) {
					}
				}
			});
			t.start();
		} catch (Exception ex) {
			System.err.println("Exception " + ex);
		}
		System.out.println(" End ");
	}
}
